from django.shortcuts import render
from django.conf import settings
from django.http import HttpResponse,JsonResponse
from booktest.models import AreaInfo,PicTest


def blocked_ips(view_func):
    def wrapper(request, *view_args, **view_kwargs):
        user_ip = request.META['REMOTE_ADDR']
        if user_ip in EXCLUDE_IPS: #['127.0.0.1']
            return HttpResponse('<h1>您的ip被禁止</h1>')
        else:
            return view_func(request, *view_args, **view_kwargs)
    return wrapper



# /static_test
# @blocked_ips
def static_test(request):
    '''静态文件'''
    print(settings.STATICFILES_FINDERS)
    return render(request,'booktest/static_test.html')

EXCLUDE_IPS = ['127.0.0.1']

# @blocked_ips
def index(request):
    user_ip = request.META['REMOTE_ADDR']
    print(user_ip)
    # num = 'a' + 1
    return render(request,'booktest/index.html')


def show_upload(request):
    return render(request,'booktest/upload_pic.html')

def upload_handle(request):
    '''上传图片处理'''
    # 1.拿到上传文件的处理对象
    pic = request.FILES['pic']
    # print(pic.name)
    # print(pic.chunks)
    # 2.创建文件
    save_path = '%s/booktest/%s'%(settings.MEDIA_ROOT,pic.name)
    with open(save_path,'wb') as f:

        # 3.获取上传文件的内容,并写到创建的文件中
        for content in pic.chunks():
            f.write(content)

    # 4.在数据库中保存上传记录
    PicTest.objects.create(good_pic='booktest/%s'%pic.name)
    # 5.返回
    return HttpResponse('ok')

# insert into booktest_areainfo value('110000','京城',NULL);
# insert into booktest_areainfo value('110100','北京市','110000');
# insert into booktest_areainfo value('110001','东城区','110100');
# insert into booktest_areainfo value('110002','西城区','110100');
# insert into booktest_areainfo value('110003','丰台区','110100');

# 前段访问需要传递页码
from django.core.paginator import Paginator
def show_area(request,pindex):
    '''分页'''
    # 1. 查询信息
    areas = AreaInfo.objects.filter() #aParent__isnull=True 父级为空
    print(areas)

    # 2. 分页，每条显示2条数据
    paginator = Paginator(areas, 2)

    # print(paginator.num_pages) # 3
    # print(paginator.page_range) # range(1, 4)

    # 3. 获取第pindex页内容
    if pindex == '':
        # 默认取第一页
        pindex=1
    else:
        pindex = int(pindex)
    page =paginator.page(pindex) #返回

    # return render(request,'booktest/show_area.html',{'areas':areas})
    return render(request,'booktest/show_area.html',{'page':page})


# 级联菜单
def getAreas(request):
    return render(request,'booktest/areas.html')

#获得省份
def getProvince(request):
    provinces = AreaInfo.objects.filter()# aParent__isnull = True
    res = []
    for i in provinces:
        res.append( [i.id , i.atitle] )
    print(res)
    return JsonResponse({'provinces':res})

#获得城市
def getCity(request):
    city_id = request.GET.get('city_id')
    cities = AreaInfo.objects.filter(aParent_id=city_id)
    res = []
    for i in cities:
        res.append([i.id, i.atitle])
    return JsonResponse({'cities':res})

#获得区 县
def getDistrict(request):
    district_id = request.GET.get('district_id')
    cities = AreaInfo.objects.filter(aParent_id=district_id)
    res = []
    for i in cities:
        res.append([i.id, i.atitle])
    return JsonResponse({'district': res})



